# Dockerfile - Debian 10 Buster - DEB version
# https://github.com/openresty/docker-openresty

ARG RESTY_IMAGE_BASE="debian"
ARG RESTY_IMAGE_TAG="buster-slim"

FROM ${RESTY_IMAGE_BASE}:${RESTY_IMAGE_TAG}

LABEL maintainer="Dodotry <dodotry@aliyun.com>"

# RESTY_DEB_FLAVOR build argument is used to select other
# OpenResty Debian package variants.
# For example: "-debug" or "-valgrind"
ARG RESTY_DEB_FLAVOR=""
ARG RESTY_DEB_VERSION="=1.19.3.1-1~buster1"
ARG RESTY_IMAGE_BASE="debian"
ARG RESTY_IMAGE_TAG="buster-slim"

LABEL resty_image_base="${RESTY_IMAGE_BASE}"
LABEL resty_image_tag="${RESTY_IMAGE_TAG}"
LABEL resty_deb_flavor="${RESTY_DEB_FLAVOR}"
LABEL resty_deb_version="${RESTY_DEB_VERSION}"

COPY conf/sources.list /usr/local/src/
COPY conf/favicon.ico /usr/local/src/
COPY conf/handle_cors.lua /usr/local/src/
COPY conf/nginx.vh.default.conf /usr/local/src/default.conf
COPY conf/start.sh /usr/local/bin/start.sh
RUN chmod a+x /usr/local/bin/start.sh
RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak
RUN cp /usr/local/src/sources.list /etc/apt/sources.list

ARG DEBIAN_FRONTEND=noninteractive
RUN /usr/sbin/groupadd nginx && /usr/sbin/useradd -g nginx nginx -s /sbin/nologin
#RUN apt install --assume-yes --no-install-recommends apt-utils
RUN echo 'debconf debconf/frontend select Noninteractive' | debconf-set-selections
RUN ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Run dpkg-reconfigure -f noninteractive tzdata
RUN DEBIAN_FRONTEND=noninteractive apt-get update && DEBIAN_FRONTEND=noninteractive apt-get upgrade -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y curl vim less
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        ca-certificates \
        gettext-base \
        gnupg2 \
        lsb-base \
        lsb-release \
        software-properties-common \
        wget \
    && wget -qO /tmp/pubkey.gpg https://openresty.org/package/pubkey.gpg \
    && DEBIAN_FRONTEND=noninteractive apt-key add /tmp/pubkey.gpg \
    && rm /tmp/pubkey.gpg \
    && DEBIAN_FRONTEND=noninteractive add-apt-repository -y "deb http://openresty.org/package/debian $(lsb_release -sc) openresty" \
    && DEBIAN_FRONTEND=noninteractive apt-get remove -y --purge \
        gnupg2 \
        lsb-release \
        software-properties-common \
        wget \
    && DEBIAN_FRONTEND=noninteractive apt-get update \
    && DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
        openresty${RESTY_DEB_FLAVOR}${RESTY_DEB_VERSION} \
    && DEBIAN_FRONTEND=noninteractive apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/* \
    && mkdir -p /var/run/openresty \
    && ln -sf /dev/stdout /usr/local/openresty${RESTY_DEB_FLAVOR}/nginx/logs/access.log \
    && ln -sf /dev/stderr /usr/local/openresty${RESTY_DEB_FLAVOR}/nginx/logs/error.log

# Add additional binaries into PATH for convenience
ENV PATH="$PATH:/usr/local/openresty${RESTY_DEB_FLAVOR}/luajit/bin:/usr/local/openresty${RESTY_DEB_FLAVOR}/nginx/sbin:/usr/local/openresty${RESTY_DEB_FLAVOR}/bin"
RUN apt-get clean
# Copy nginx configuration files
VOLUME /etc/nginx/conf.d
COPY conf/nginx.conf /usr/local/openresty${RESTY_DEB_FLAVOR}/nginx/conf/nginx.conf
COPY conf/nginx.vh.default.conf /etc/nginx/conf.d/default.conf
COPY conf/handle_cors.lua /etc/nginx/conf.d/handle_cors.lua
RUN mkdir -p /etc/nginx/conf.d/certs
COPY conf/favicon.ico /usr/local/openresty${RESTY_DEB_FLAVOR}/nginx/html/
RUN mkdir -p /var/log/nginx && chmod 755 -R /var/log/nginx && chown -R nginx.nginx /var/log/nginx

#CMD ["/usr/bin/openresty", "-g", "daemon off;"]
CMD ["/usr/local/bin/start.sh"]
EXPOSE 80 443 8080

# Use SIGQUIT instead of default SIGTERM to cleanly drain requests
# See https://github.com/openresty/docker-openresty/blob/master/README.md#tips--pitfalls
STOPSIGNAL SIGQUIT

